package huaweiod.third;

import java.util.*;

/**
 * 文件目录大小
 * https://blog.csdn.net/qfc_128220/article/details/130765017?spm=1001.2014.3001.5501
 */
public class B_FileDirectorySize {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int parentId = in.nextInt();

        Map<Integer, Set<Integer>> fileMap = new HashMap<>();
        Map<Integer, Integer> sizeMap = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int id = in.nextInt();
            int size = in.nextInt();
            sizeMap.put(id, size);
            String childStr = in.next();
            Set<Integer> child = new HashSet<>();
            if (childStr.length() > 2) {
                String[] split = childStr.substring(1, childStr.length() - 1).split(",");
                for (String s : split) {
                    child.add(Integer.valueOf(s));
                }
            }
            fileMap.put(id, child);
        }

        int ans = dfs(parentId, fileMap, sizeMap);
        System.out.println(ans);
    }

    private static int dfs(int parentId, Map<Integer, Set<Integer>> fileMap, Map<Integer, Integer> sizeMap) {
        int size = sizeMap.get(parentId);
        Set<Integer> childSet = fileMap.get(parentId);
        for (Integer childId : childSet) {
            size += dfs(childId, fileMap, sizeMap);
        }
        return size;
    }
}
